package com.by_man.presenter.main_page;
/*
* 主界面
* by man
* 2017-01-31
 */
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

import com.by_man.presenter.R;
import com.by_man.presenter.SharedApplication;
import com.by_man.presenter.menu_activity.HumanServiceActivity;
import com.by_man.presenter.menu_activity.PublicNumberActivity;
import com.by_man.presenter.menu_activity.ServiceIntroductionActivity;
import com.by_man.presenter.menu_activity.SettingActivity;
import com.by_man.presenter.module_gestureLock.GestureLockActivity;
import com.by_man.presenter.module_health_consultant.HealthConsultantFragment;
import com.by_man.presenter.module_health_management.HealthManagementFragment;
import com.by_man.presenter.module_health_records.HealthRecordsFragment;
import com.by_man.presenter.module_member_activity.MemberActivityFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int position = getIntent().getIntExtra("position", 0);
        initView(position);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){

            case R.id.menu_serveintroduce:
                Intent intent_A = new Intent(this, ServiceIntroductionActivity.class);
                startActivity(intent_A);
                break;
            case R.id.menu_publicnumber:
                Intent intent_B = new Intent(this, PublicNumberActivity.class);
                startActivity(intent_B);
                break;
            case R.id.menu_articalserve:
                Intent intent_C = new Intent(this, HumanServiceActivity.class);
                startActivity(intent_C);
                break;
            case R.id.menu_setting:
                Intent intent_D = new Intent(this, SettingActivity.class);
                startActivity(intent_D);
                break;
            case R.id.menu_exit:
                showTips();
                break;

        }
        return super.onOptionsItemSelected(item);
    }

    private void showTips() {
        AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("提醒")
                .setMessage("是否退出程序")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        finishAffinity();
                    }

                }).setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                return;
                            }
                        }).create(); // 创建对话框
        alertDialog.show(); // 显示对话框
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK)
            showTips();
        return super.onKeyDown(keyCode,event);
    }

    private void initView(int position){
        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        // 初始化ToolBar
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.logotitle);
        actionBar.setDisplayHomeAsUpEnabled(true);

        //初始化TabLayout
        TabLayout tabLayout = (TabLayout)findViewById(R.id.tab_layout);
        List<String> titles = new ArrayList<>();
        titles.add("健康管理");
        titles.add("健康顾问");
        titles.add("健康档案");
        titles.add("会员活动");
        for(int i=0;i<titles.size();i++){
            tabLayout.addTab(tabLayout.newTab().setText(titles.get(i)));
        }

        //初始化viewpage
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        List<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(new HealthManagementFragment());fragments.add(new HealthConsultantFragment());
        fragments.add(new HealthRecordsFragment());fragments.add(new MemberActivityFragment());

        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(adapter);
        //将Tablayout与viewpager关联
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabsFromPagerAdapter(adapter);
        viewPager.setCurrentItem(position);
    }

    protected void onRestart()
    {
        super.onRestart();
        Intent intent=new Intent(this,GestureLockActivity.class);
        intent.putExtra("class",this.getClass().getName());
        intent.putExtra("position", viewPager.getCurrentItem());
        startActivity(intent);
    }

}
